﻿
using System;
using System.Collections.Generic;
using System.Globalization;
using Engine;
using Engine.Graphics;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class SubsystemWorldSystem : Subsystem, IDrawable
    {
        public readonly PrimitivesRenderer3D m_primitivesRenderer3D = new PrimitivesRenderer3D();

        public SubsystemBodies m_subsystemBodies;

        public SubsystemPickables m_subsystemPickables;

        public Dictionary<int, GamePlayerDataInfo> m_playerinfo = new Dictionary<int, GamePlayerDataInfo>();

        public static int[] m_drawOrders = new int[1] { 200 };

        public int[] DrawOrders => m_drawOrders;

        public GamePlayerDataInfo GetPlayerInfo(int playerIndex)
        {
            if (!m_playerinfo.TryGetValue(playerIndex, out var value))
            {
                value = new GamePlayerDataInfo();
                m_playerinfo.Add(playerIndex, value);
            }

            return value;
        }

        public void Draw(Camera camera, int drawOrder)
        {
            if (ModItemManger.CreaterMode)
            {
                try
                {
                    foreach (ComponentBody componentBody in m_subsystemBodies.m_componentBodies)
                    {
                        FlatBatch3D flatBatch3D = m_primitivesRenderer3D.FlatBatch();
                        flatBatch3D.QueueBoundingBox(componentBody.BoundingBox, ModItemManger.MainUiColor);
                        m_primitivesRenderer3D.Flush(camera.ViewProjectionMatrix);
                    }

                    return;
                }
                catch (Exception ex)
                {
                    Log.Information(ex.ToString());
                    return;
                }
            }

            m_primitivesRenderer3D.Clear();
        }

        public override void Load(ValuesDictionary valuesDictionary)
        {
            m_subsystemBodies = base.Project.FindSubsystem<SubsystemBodies>(throwOnError: true);
            m_subsystemPickables = base.Project.FindSubsystem<SubsystemPickables>(throwOnError: true);
            foreach (KeyValuePair<string, object> item in valuesDictionary.GetValue("GameInfo", new ValuesDictionary()))
            {
                GamePlayerDataInfo gamePlayerDataInfo = new GamePlayerDataInfo();
                gamePlayerDataInfo.Load((ValuesDictionary)item.Value);
                m_playerinfo.Add(int.Parse(item.Key, CultureInfo.InvariantCulture), gamePlayerDataInfo);
            }
        }

        public override void Save(ValuesDictionary valuesDictionary)
        {
            ValuesDictionary valuesDictionary2 = new ValuesDictionary();
            valuesDictionary.SetValue("GameInfo", valuesDictionary2);
            foreach (KeyValuePair<int, GamePlayerDataInfo> item in m_playerinfo)
            {
                ValuesDictionary valuesDictionary3 = new ValuesDictionary();
                valuesDictionary2.SetValue(item.Key.ToString(CultureInfo.InvariantCulture), valuesDictionary3);
                item.Value.Save(valuesDictionary3);
            }
        }
    }
}